package com.wcansoft.baseWeb.mybatis_plugin.dao;

import java.util.Arrays;
import java.util.List;

/**
 * 查询条件类
 * 鲍庆丰
 * baoqingfeng@gmail.com
 * 2007-3-27
 */
public class Query {
	private ConditionValue conditionCV;

	/**
	 * 构造任意查询条件
	 * <br>如：new Query(" id>? and name=? and passwd=? and state=?",12,"bao","135246",0)
	 * @param conditionvalue 任意长度条件
	 */
	public Query(Object ... conditionvalue)
	{
		if(conditionvalue==null || conditionvalue.length==0)
			return;
		if(conditionvalue.length==1)
			this.conditionCV = new ConditionValue((String)conditionvalue[0]);

		Object dest[] = new Object[conditionvalue.length-1];
		System.arraycopy(conditionvalue, 1, dest, 0, conditionvalue.length-1);
		this.conditionCV = new ConditionValue((String)conditionvalue[0],dest);
	}
	/**
	 * 构造查询条件
	 * <br>如：new Query(" where id>1")
	 * @param pre_condition 需要带where
	 */
	public Query(String pre_condition)
	{
		this.conditionCV = new ConditionValue(pre_condition);
	}
	/**
	 * 构造查询条件
	 * <br>如：new Query(" where id>?",id)
	 * @param pre_condition 条件
	 * @param value 值
	 */
	public Query(String pre_condition,Object value)
	{
		this.conditionCV = new ConditionValue(pre_condition,value);
	}
	/**
	 * 构造查询条件
	 * <br>如：new Query(" where id>? and name like ?",id,"%"+name+"%")
	 * @param pre_condition 条件
	 * @param value1 条件中的第一个值
	 * @param value2 条件中的第二个值
	 */
	public Query(String pre_condition,Object value1,Object value2)
	{
		this.conditionCV = new ConditionValue(pre_condition,value1,value2);
	}
	/**
	 * 构造查询条件
	 * <br>如：new Query(" where id>? and name like ? and type=?",id,"%"+name+"%",2)
	 * @param pre_condition 条件
	 * @param value1 条件中的第一个值
	 * @param value2 条件中的第二个值
	 * @param value3 条件中的第三个值
	 */
	public Query(String pre_condition,Object value1,Object value2,Object value3)
	{
		this.conditionCV = new ConditionValue(pre_condition,value1,value2,value3);
	}
	/**
	 * 构造查询条件
	 * <br>如：new Query(" where id>? and name like ? and type=? and sex=?",new Object[]{id,name,2,0})
	 * @param pre_condition 条件
	 * @param values 值数组
	 */
	public Query(String pre_condition,Object values[])
	{
		this.conditionCV = new ConditionValue(pre_condition,values);
	}
	/**
	 * 构造查询条件
	 * <br>如：new Query(new ConditionValue(" where id>? and name like ? and type=? and sex=? and addr like ?",id,name,2,0,addr))
	 * @param condition 条件和值对象，可以任意多个值
	 */
	public Query(ConditionValue condition)
	{
		this.conditionCV = condition;
	}

	public ConditionValue getConditionValue() {
		return conditionCV;
	}

	public List<Object> getVals() {
		return Arrays.asList(conditionCV.getVals());
	}

	public String getCondition() {
		if(conditionCV==null)
			return "";
		return conditionCV.getCondition();
	}
}
